package com.nuanshui.heatedloan.web.accountbill;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Maps;
import com.nuanshui.framework.lang.MoneyUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.fund.enums.PaymentBizType;
import com.nuanshui.heatedloan.fund.facade.UserAccountBillService;
import com.nuanshui.heatedloan.fund.facade.UserFundActionService;
import com.nuanshui.heatedloan.fund.form.UserAccountBillForm;
import com.nuanshui.heatedloan.fund.model.UserAccountBill;
import com.nuanshui.heatedloan.fund.vo.UserAccountBillVO;
import com.nuanshui.heatedloan.msgcenter.constant.SmsMsgBizType;
import com.nuanshui.heatedloan.msgcenter.integration.MsgTools;
import com.nuanshui.heatedloan.msgcenter.model.AliSmsNotificationCodeTemp;
import com.nuanshui.heatedloan.msgcenter.model.SmsPushMsgBody;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.web.ApiController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cms/manage/accountbill")
public class AccountBillController extends ApiController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
	private UserAccountBillService userAccountBillService;
	
	@Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserFundActionService userFundActionService;

	@Autowired
    private MsgTools msgTools;

	@RequestMapping("/list")
	public String list(Pager<UserAccountBillVO> pager ,String query){
		Map<String, Object> map = Maps.newHashMap();
		UserAccountBillForm uabf = null;
		if(StringUtils.isNotEmpty(query) && !DEFAULTQUERYSTR.equals(query)){
			uabf = JSON.parseObject(query, UserAccountBillForm.class);
			if (uabf.getCreatedAtEnd() != null) {
				long time = uabf.getCreatedAtEnd().getTime()+86400000;
				Date date = new Date(time);
				uabf.setCreatedAtEnd(date);
			}
		}
		pager = userAccountBillService.list(pager, uabf);
		List<UserAccountBillVO> data = pager.getData();
		map.put("list", data);
		map.put("total", pager.getTotal());
		return success(map);
	}
	
	@RequestMapping("/getDes")
	public String getDes(String dn){
		String des = userAccountBillService.getDes(dn);
		return success(des);
	}
	
	@RequestMapping("/modifyDes")
	public String modifyDes(String dn, String newDes){
		userAccountBillService.modifyDes(newDes, dn);
		return success();
	}
	
	@RequestMapping("/modifyStatus")
	public String modifyStatus(String dn, int status){
		UserAccountBill userAccount = userAccountBillService.findBillDetailByDealNum(dn);
		userFundActionService.checkUserCashing(null, status, userAccount.getId());
		sendMsg(userAccount.getId(),status);
		return success();
	}
	
	@RequestMapping("/getAccountMessage")
	public String getAccountMessage(String dn) {
		UserAccountBill userAccountBill = userAccountBillService.getAccountMessage(dn);
		return success(userAccountBill);
	}
	
	private void sendMsg(String billId, Integer isPass) {
        LogUtil.info(logger, "根据isPass:" + isPass + "来判断是否发送短信");
        UserAccountBill userAccountBill = userAccountBillService.findBillDetail(billId);
        SmsPushMsgBody model = new SmsPushMsgBody();
        model.addDstUserId(userAccountBill.getUserId());
        model.setBizId(userAccountBill.getBizId());
        model.putParam("money", MoneyUtil.toDoubleString(userAccountBill.getMoney() * -1));
        if (PaymentBizType.WITHDRAW_CASH.getCode().equals(userAccountBill.getType())) {
            if (isPass == 1) {

				model.setBizType(SmsMsgBizType.APPLY_DRAW_CASH_OK);
				model.setTemplateCode(AliSmsNotificationCodeTemp.APPLY_DRAW_CASH_OK.gettempCode());
                //如果是提现审核成功发送短信
                msgTools.sendSmsMsg(model);
            } else if (isPass == 0) {//不通过发送短息

				model.setBizType(SmsMsgBizType.APPLY_DRAW_CASH_REFUSE);
				model.setTemplateCode(AliSmsNotificationCodeTemp.APPLY_DRAW_CASH_REFUSE.gettempCode());
				msgTools.sendSmsMsg(model);
            }
        }
    }
}
